package com.saybiu.service.serviceImpl;

import com.saybiu.dao.*;
import com.saybiu.domain.*;
import com.saybiu.exception.ServiceException;
import com.saybiu.response.CommonCode;
import com.saybiu.service.PostService;
import com.saybiu.utils.Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: liming
 * @Date: 2021/4/18 18:05
 */
@Service
public class PostServiceImpl implements PostService {
    @Resource
    private PostDao postDao;

    @Resource
    private PostMetaDao postMetaDao;

    @Resource
    private PictureDao pictureDao;

    @Resource
    private UserMetaDao userMetaDao;

    @Resource
    private LikeDao likeDao;

    @Resource
    private CollectionDao collectionDao;


    /**
     * 解决上传图片时的缓存问题
     * @param pictureList  图片集合
     * @param picturePath 图片存储的路径
     * @return
     */
    @Override
    public List<Picture> addPicture(List<Picture> pictureList, String picturePath) {
        List<Picture> listPicture = new ArrayList<>();
        for (Picture p: pictureList) {
            Picture picture = new Picture();
            picture.setPictureUrl(picturePath + "/"  +p.getPictureName());
            picture.setPictureName((p.getPictureName()).substring(0,(p.getPictureName()).lastIndexOf(".")));
            picture.setUserId(p.getUserId());
            listPicture.add(picture);
        }

        int numPicture =  pictureDao.addPictureDao(listPicture);
        if(numPicture <= 0 ){
            throw new ServiceException(CommonCode.INSERT_ERROR);
        }
        return listPicture;
    }

    /**
     * 用户发送帖子
     * @param post 帖子目录信息
     * @param postMeta 帖子内容信息
     * @param picture 图片id数组
     * 做事务处理
     */
    @Override
    public void addUserSendPost(Post post, PostMeta postMeta,Integer[] picture) {
        //添加帖子目录表
        int numPost = postDao.addPostDao(post);
        //添加帖子内容表
        int numPostMeta = postMetaDao.addPostMetaDao(post.getPostId(),postMeta);
        if(numPost <= 0 || numPostMeta <= 0){
            throw new ServiceException(CommonCode.INSERT_ERROR);
        }
        //激活帖子中的图片
        if(picture != null){ //当用户发送帖子有图片时,再去图片库激活贴子所带图片.
            int numPicture = pictureDao.updatePicturePostId(post.getPostId(),post.getUserId(),picture);
            if( numPicture <= 0) {
                throw new ServiceException(CommonCode.UPDATE_ERROR);
            }
        }
    }

    /**
     * 用户删除帖子
     * @param userId 用户id
     * @param postId 帖子目录id
     * 做事务处理
     */
    @Override
    public void deleteUserPost(Integer userId, Integer postId) {
        //逻辑删除帖子目录 是否被封禁(1/0)
        int numPost = postDao.deleteUserPost(userId,postId);

        //userMeta表的user_post_num 主动发帖数
        int numUserMeta = userMetaDao.updateUserPostNum(userId);

        if(numPost <= 0 || numUserMeta <= 0){
            throw new ServiceException(CommonCode.DELETE_ERROR);
        }
    }

    /**
     * 阅读帖子 帖子阅读量自增 1
     * @param postId 帖子id
     * 操作bbs_posts表
     */
    @Override
    public void readPost(Integer postId) {
        int numReadPost = postDao.addReadPost(postId);
        if(numReadPost <= 0){
            throw new ServiceException(CommonCode.UPDATE_ERROR);
        }
    }

    /**
     * 给帖子点赞 帖子点赞量自增 1
     * @param userId 用户id
     * @param postId 帖子id
     * @param postUrl 帖子路径
     * 做事务处理
     * 操作bbs_posts、bbs_likes表
     */
    @Override
        public void likePost(Integer userId, Integer postId,String postUrl) {
        //查询用户是否点赞重复
        Like like = likeDao.selectLikeRepeat(userId,postId);
        if(like == null){
            //用户点赞之后,往数据库点赞表插入一条点赞数据
            String likeTime = Utils.toDate(new Date());
            int numLikePost = likeDao.addLikePost(userId,postId,postUrl,likeTime);
            //更新帖子点赞数量
            int numPostLikeNum = postDao.updateLikeNum(postId);
            if(numLikePost <= 0 || numPostLikeNum <= 0){
                throw new ServiceException(CommonCode.UPDATE_ERROR);
            }
        }else {
            //点赞重复
            throw  new ServiceException(CommonCode.INSERT_REPEAT);
        }

    }

    /**
     * 取消点赞 删除点赞表用户点赞记录 , 帖子目录表 点赞量-1
     * @param userId 用户id
     * 做事务处理
     * @param postId 帖子id
     */
    @Override
    public void cancelLikePost(Integer userId, Integer postId) {
        //删除点赞表用户点赞记录
        int numLikePost = likeDao.deleteLikePost(userId,postId);

        //帖子目录表点赞量-1
        int numPostLikeNum = postDao.updatePostLikeNum(postId);

        if(numLikePost <= 0 || numPostLikeNum <= 0){
            throw new ServiceException(CommonCode.UPDATE_ERROR);
        }
    }

    /**
     * 收藏帖子 帖子收藏量自增 1
     * @param userId 用户id
     * @param postId 帖子id
     * @param postUrl 帖子路径
     * 做事务处理
     * 操作bbs_userMeta(user_support_post_num), bbs_posts(Post_collected_num)、bbs_collections表
     */
    @Override
    public void collectionPost(Integer userId, Integer postId,String postUrl) {
        //查询用户是否已经收藏帖子
        Collection collection = collectionDao.selectConllection(userId,postId);
        if(collection == null){
            //添加收藏表
            int numCollection = collectionDao.addCollection(userId,postId,postUrl,Utils.toDate(new Date()));
            if(numCollection <= 0 ){
                throw new ServiceException(CommonCode.INSERT_ERROR);
            }
            //更新收藏量
            int numPostCollectedNum = postDao.updatePostCollectedNumAdd(postId);
            //更新用户收藏数
            int numUserMeta = userMetaDao.updateUserSupportPostNumAdd(userId);

            if(numPostCollectedNum <= 0 || numUserMeta <= 0){
                throw new ServiceException(CommonCode.UPDATE_ERROR);
            }
        }else {
            throw new ServiceException(CommonCode.INSERT_REPEAT);
        }

    }

    /**
     * 删除已收藏帖子
     * @param userId 用户id
     * @param postId 帖子id
     * 做事务处理
     * 影响bbs_userMeta(user_support_post_num), bbs_posts(Post_collected_num)、bbs_collections表
     */
    @Override
    public void deleteUserSupportPost(Integer userId, Integer postId) {
        //删除收藏表,此帖子信息
        int numDeleteUserSupportPost = collectionDao.deleteUserSupportPost(userId,postId);

        if(numDeleteUserSupportPost <= 0 ){
            throw new ServiceException(CommonCode.DELETE_ERROR);
        }
        //更新此帖子的收藏量
        int numPostCollectedNumSub =  postDao.updatePostCollectedNumSub(postId);

        //更新用户收藏帖子量
        int numUserSupportPostNumSub = userMetaDao.updateUserSupportPostNumSub(userId);

        if(numPostCollectedNumSub <=0 || numUserSupportPostNumSub <=0){
            throw new ServiceException(CommonCode.UPDATE_ERROR);
        }
    }

    //-------------------------对帖子的查询操作-------------------------

    /**
     * 用户查看自己发送的帖子
     * @param userId 用户id
     * @return 帖子集合
     */
    @Override
    public List<Post> queryUserPost(Integer userId) {
        List<Post> listUserPost = postDao.queryUserPost(userId);
        if(listUserPost != null){
            return listUserPost;
        }else {
            throw new ServiceException(CommonCode.SELECT_ERROR);
        }
    }

    /**
     * 用户查看自己发送的帖子的具体内
     * @param postId 帖子id
     * @return 帖子内容对象
     */
    @Override
    public PostMeta queryUserPostMeta(Integer postId) {
        //查询帖子内容
        PostMeta postMeta = postMetaDao.queryPostMeta(postId);
        if(postMeta != null){
            return postMeta;
        }else {
            throw new ServiceException(CommonCode.SELECT_ERROR);
        }
    }

    /**
     * 根据帖子类型和阅读量查询
     * @param typeName 帖子类型
     * @retur 帖子表的集合
     */
    @Override
    public List<Post> queryPostsByTypeAndReadNum(String typeName) {
        List<Post> listPost = postDao.queryPostsByTypeAndReadNum(typeName);
        if(listPost != null){
            return listPost;
        }else {
            throw new ServiceException(CommonCode.SELECT_ERROR);
        }
    }

    /**
     * 根据帖子类型和点赞量查询
     * @param typeName 帖子类型
     * @return
     * 操作bbs_posts表
     */
    @Override
    public List<Post> queryPostByTypeAndLikeNum(String typeName) {
        List<Post> listPost = postDao.queryPostByTypeAndLikeNum(typeName);
        if(listPost != null){
            return listPost;
        }else {
            throw new ServiceException(CommonCode.SELECT_ERROR);
        }
    }

    /**
     * 根据帖子类型和收藏量查询
     * @param typeName 帖子类型
     * @return
     * 操作bbs_posts表
     */
    @Override
    public List<Post> queryPostByTypeAndCollectionNum(String typeName) {
        List<Post> listPost = postDao.queryPostByTypeAndCollectionNum(typeName);
        if(listPost != null){
            return listPost;
        }else {
            throw new ServiceException(CommonCode.SELECT_ERROR);
        }
    }

    /**
     * 根据阅读量查询帖子
     * @return
     * 操作bbs_posts表
     */
    @Override
    public List<Post> queryPostByReadNum() {
        List<Post> listPost = postDao.queryPostByReadNum();
        if(listPost != null){
            return listPost;
        }else {
            throw new ServiceException(CommonCode.SELECT_ERROR);
        }
    }

    /**
     * 根据点赞量查询帖子
     * @return
     * 操作bbs_posts表
     */
    @Override
    public List<Post> queryPostByLikeNum() {
        List<Post> listPost = postDao.queryPostByLikeNum();
        if(listPost != null){
            return listPost;
        }else {
            throw new ServiceException(CommonCode.SELECT_ERROR);
        }
    }

    /**
     * 根据收藏量查询帖子
     * @return
     * 操作bbs_posts表
     */
    @Override
    public List<Post> queryPostByCollectionNum() {
        List<Post> listPost = postDao.queryPostByCollectionNum();
        if(listPost != null){
            return listPost;
        }else {
            throw new ServiceException(CommonCode.SELECT_ERROR);
        }
    }
}
